package com.ssy.lingxi.member.merchant.serviceimpl.feign;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.service.feign.IPlatformManageFeignService;
import com.ssy.lingxi.platform.manage.api.feign.inner.PlatformParameterManageControllerFeign;
import com.ssy.lingxi.platform.manage.api.feign.inner.SensitiveWordControllerFeign;
import com.ssy.lingxi.platform.manage.api.feign.inner.SiteControllerFeign;
import com.ssy.lingxi.platform.manage.api.model.dto.sensitive.SensitiveWordFilterDTO;
import com.ssy.lingxi.platform.manage.api.model.dto.sensitive.SensitiveWordListFilterDTO;
import com.ssy.lingxi.platform.manage.api.model.dto.site.SiteIdDTO;
import com.ssy.lingxi.platform.manage.api.model.vo.parameter.PlatformParameterManageVO;
import com.ssy.lingxi.platform.manage.api.model.vo.sensitive.SensitiveWordFilterVO;
import com.ssy.lingxi.platform.manage.api.model.vo.sensitive.SensitiveWordListFilterVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 调用平台管理服务Feign接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2020-12-03
 */
@Slf4j
@Service
public class PlatformManageFeignServiceImpl implements IPlatformManageFeignService {
    @Resource
    private SensitiveWordControllerFeign sensitiveWordControllerFeign;

    @Resource
    private PlatformParameterManageControllerFeign platformParameterManageControllerFeign;

    @Resource
    private SiteControllerFeign siteControllerFeign;

    /**
     * 敏感词过滤
     * @param sentence 要过滤的语句
     * @return 过滤后的语句
     */
    @Override
    public Wrapper<String> filterSensitiveWord(String sentence) {
        SensitiveWordFilterDTO sensitiveWordRequest = new SensitiveWordFilterDTO();
        sensitiveWordRequest.setContent(sentence);
        Wrapper<SensitiveWordFilterVO> sensitiveWordResponse = sensitiveWordControllerFeign.filterSensitiveWord(sensitiveWordRequest);
        if (ResponseCode.SUCCESS.getCode() != sensitiveWordResponse.getCode()) {
            return Wrapper.fail(ResponseCode.SERVICE_PLATFORM_MANAGE_ERROR);
        }

        return Wrapper.success(sensitiveWordResponse.getData().getContent());
    }

    /**
     * 敏感词过滤
     * @param sentenceList 要过滤的语句列表
     * @return 过滤后的语句列表
     */
    @Override
    public Wrapper<List<String>> filterSensitiveWordList(List<String> sentenceList) {
        SensitiveWordListFilterDTO sensitiveWordRequest = new SensitiveWordListFilterDTO();
        sensitiveWordRequest.setContents(sentenceList);
        Wrapper<SensitiveWordListFilterVO> sensitiveWordResponse = sensitiveWordControllerFeign.filterSensitiveWordList(sensitiveWordRequest);
        if (ResponseCode.SUCCESS.getCode() != sensitiveWordResponse.getCode()) {
            return Wrapper.fail(ResponseCode.SERVICE_PLATFORM_MANAGE_ERROR);
        }

        return Wrapper.success(sensitiveWordResponse.getData().getContents());
    }

    /**
     * 查询平台参数配置
     * @param code 参数编码
     * @return 返回结果
     */
    @Override
    public Wrapper<PlatformParameterManageVO> parameterManageDetails(String code) {
        try {
            return platformParameterManageControllerFeign.parameterManageDetails(code);
        } catch (Exception e) {
            log.error("查询平台参数配置失败", e);
        }

        return Wrapper.fail("查询平台参数配置失败");
    }

    @Override
    public Wrapper<Boolean> enableMultiTenancy(SiteIdDTO dto) {
        Wrapper<Boolean> wrapper = siteControllerFeign.enableMultiTenancy(dto);
        if (ResponseCode.SUCCESS.getCode() != wrapper.getCode()) {
            return Wrapper.fail(ResponseCode.SERVICE_PLATFORM_MANAGE_ERROR);
        }
        return wrapper;
    }
}
